package com.gitee.zhanyao.monomer.util.datatables;

import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;

public class PageUtil {

    private PageUtil() {}

    /**
     * 从 datatables 请求参数中组合排序参数
     *
     * @param dataTablesRequest datatables 请求参数
     * @return Sort
     */
    public static Sort getOrders(DataTablesRequest<?> dataTablesRequest) {
        List<DataTablesOrder> dataTablesOrders = dataTablesRequest.getOrder();
        List<DataTablesColumn> dataTablesColumns = dataTablesRequest.getColumns();
        List<Sort.Order> orders = new ArrayList<>();
        DataTablesColumn dataTablesColumn;
        Sort.Order order;
        for (DataTablesOrder dataTablesOrder : dataTablesOrders) {
            dataTablesColumn = dataTablesColumns.get(dataTablesOrder.getColumn());
            order = new Sort.Order(Sort.Direction.fromString(dataTablesOrder.getDir().name()), dataTablesColumn.getData());
            orders.add(order);
        }
        return Sort.by(orders);
    }

}
